/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package contasreceber.bd;

import conexao.ConexaoSingleton;
import conexao.DAOException;
import contasreceber.classes.ContasReceber;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Wolverine
 */
public class ContasReceberDao implements IContasReceberDao{

    EntityManager manager;

    public ContasReceberDao() throws DAOException {
        try {
            this.manager = ConexaoSingleton.getConexao();
        } catch (Exception ex) {
            throw new DAOException("Erro na conexão com Banco de Dados", ex);
        }
    }



    public void gerarConta() throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void editarConta(String idConta) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void excluirConta(ContasReceber contaReceber) throws DAOException {
        try{
            manager.getTransaction().begin();
            manager.remove(contaReceber);
            manager.getTransaction().commit();
          }catch(Exception ex){
              if (manager.getTransaction().isActive()){
                  manager.getTransaction().rollback();
              }
              throw new DAOException("Erro ao Excluir Conta Receber", ex);
          }
    }

    public ContasReceber getConta(String idConta) throws DAOException {
        Query query = manager.createQuery("Select c From ContasReceber c where c.idConta='" +  idConta + "'");
        return (ContasReceber) query.getSingleResult();
    }

    public List<ContasReceber> getContaNome(String cedente) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<ContasReceber> getContaVencendo() throws DAOException {
        Query query = manager.createQuery("Select c from ContasReceber c where c.dataVencimento='2009/11/06'");
        return query.getResultList();
    }

    public List<ContasReceber> getContaVencer() throws DAOException {
        Query query = manager.createQuery("Select c from ContasReceber c where c.dataVencimento>'2009/11/06'");
        return query.getResultList();
    }

    public List<ContasReceber> getContaVencida() throws DAOException {
        Query query = manager.createQuery("Select c from  ContasReceber c where c.dataVencimento<'2009/11/06'");
        return query.getResultList();
    }

    public double calcularVencendo() throws DAOException {
        Query query = manager.createNativeQuery("Select distinct sum(valorReceber) as tvalor from contasreceber where dataVencimento='2009/11/06' and valorRecebido=0");
        return (Double) query.getSingleResult();
    }

    public double calcularVencer() throws DAOException {
        Query query = manager.createNativeQuery("Select distinct sum(valorReceber) as tvalor from contasreceber where dataVencimento>'2009/11/06' and valorRecebido=0");
        return (Double) query.getSingleResult();
    }

    public double calcularVencidas() throws DAOException {
        Query query = manager.createNativeQuery("Select distinct sum(valorReceber) as tvalor from contasreceber where dataVencimento<'2009/11/06' and valorRecebido=0");
        return (Double) query.getSingleResult();
    }

    public List<ContasReceber> pesquisaDataEmissao(Date dataInicio, Date dataFinal) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<ContasReceber> pesquisaDataVencimento(Date dataInicio, Date dataFinal) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<ContasReceber> pesquisaValorReceber(double valor, int indice) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<ContasReceber> pesquisaTipoConta(int tipo) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   


}
